package com.mms.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mms.dao.IMedicalHistoryDao;
import com.mms.enumerate.ReserveFlowProgress;
import com.mms.pojo.*;
import com.mms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 陈林鹏
 * @since 2021-04-14
 */
@Service
public class MedicalHistoryServiceImpl extends ServiceImpl<IMedicalHistoryDao, MedicalHistory> implements IMedicalHistoryService {

    /**
     * 医务服务
     */
    @Autowired
    IMedicalService medicalService;

    /**
     * 用户信息服务
     */
    @Autowired
    IUserInfoService userInfoService;

    /**
     * 预约 服务
     */
    @Autowired
    IReserveService reserveService;

    /**
     * 药品表服务
     */
    @Autowired
    IMedicineService medicineService;


    /**
     * 结束看诊
     * 创建历史记录并记录病床id
     *
     * @param userinfo
     * @param medicineInfoJson
     * @param diagnosisResult
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED
            , propagation = Propagation.REQUIRED
            , rollbackFor = Exception.class)
    public boolean endVisit(UserInfo userinfo,
                            String medicineInfoJson,
                            String diagnosisResult,
                            Integer reserveId) throws Exception {
        //获取医务信息
        Medical medical = medicalService.getMedicalByUserInfo(userinfo);
        //定义病历表
        MedicalHistory medicalHistory = new MedicalHistory();
        //获取预约信息
        Reserve reserve = reserveService.selectById(reserveId);
        //获取病人 信息
        UserInfo patient = userInfoService.selectById(reserve.getUserInfoId());

        //设置主治医生id
        medicalHistory.setPhysicianId(medical.getId());
        //设置诊断结果
        medicalHistory.setDiagnosisResult(diagnosisResult);

        //设置病人id
        medicalHistory.setPatientId(patient.getId());
        medicalHistory.setMedicineInfo(medicineInfoJson);

        //处理 出药
        List<Map> medicineInfoArr = JSON.parseArray(medicineInfoJson, Map.class);

        for (Map tempMedicine : medicineInfoArr) {
            Integer id = (Integer) tempMedicine.get("id");
            Integer num = (Integer) tempMedicine.get("num");
            Medicine medicine = medicineService.selectById(id);
            if (num <= 0) {
                throw new Exception("禁止取药数量低于0或等于0");
            }
            Integer numT = medicine.getInventory() - num;
            //如果药瓶不足就 抛出异常
            if (numT < 0) {
                throw new Exception("库存量不足");
            }
            //设置数量
            medicine.setInventory(numT);
            //更新药品表数据
            medicineService.updateById(medicine);
        }

        //历史记录
        insert(medicalHistory);

        //设置用户预约进度出院
        reserve.setFlowProgress(ReserveFlowProgress.CompleteVisit.getKey());
        //更新用户预约数据信息
        reserveService.updateById(reserve);

        return true;
    }

}
